package com.example.edu.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.edu.entity.Chapter;
import com.example.edu.entity.course.EduChapter;
import com.example.edu.service.ChapterService;
import com.example.utils.Result;


/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/edu")
public class ChapterController {
	
	@Autowired
	private ChapterService chapterService;
	
	@GetMapping("chapter/{courseId}")
	public Result getAllChapterVideo(@PathVariable String courseId) {
		List<EduChapter> chapters = chapterService.getAllChapterVideo(courseId);
		
		return Result.ok().data("chapters", chapters);
	}
	
	@PostMapping("chapter")
	public Result saveChapter(@RequestBody Chapter chapter) {
		
		chapterService.save(chapter);
		
		return Result.ok();	
	}
	
	@DeleteMapping("chapter/{id}")
	public Result deleteChapterById(@PathVariable String id) {
		
		chapterService.deleteById(id);
		return Result.ok();		
	}
	
	@PutMapping("chapter")
	public Result updateChapterById(@RequestBody Chapter chapter) {
		chapterService.updateById(chapter);
		return Result.ok();
	}
	
	@GetMapping("chapterById/{id}")
	public Result getChapterById(@PathVariable String id) {
		Chapter chapter = chapterService.getById(id);
		return Result.ok().data("chapter", chapter);
	}
	
	
	@GetMapping("chapter")
	public Result SelectChapter(@PathVariable String courseId) {
		List<EduChapter> chapters = chapterService.getAllChapterVideo(courseId);
		
		return Result.ok().data("chapters", chapters);
	}

}

